﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Windows.UI.Xaml.Data;

using SeriesManager.Enum;
using SeriesManager.Model;
using WinRTFramework.Interfaces.AppStates;

namespace SeriesManager.ViewModel
{
    public sealed class ActorViewModel : ViewModelBase, ISupportTransientState
    {
        #region Properties

        public ICollectionView View
        {
            get;
            private set;
        }

        #endregion

        public override void OnNavigatedTo(object navigationParameter)
        {
            base.OnNavigatedTo(navigationParameter);
            var series = AppX.Instance.Current;
            this.View = new CollectionViewSource() { Source = series.Actors }.View;
            base.OnPropertyChanged<ICollectionView>(() => View);
        }

        public object GetTransientState()
        {
            if (this.View != null && this.View.CurrentItem != null)
            {
                return (this.View.CurrentItem as ActorModel).Name;
            }
            return null;
        }

        public IEnumerable<System.Type> GetTransientStateTypes()
        {
            yield return typeof(string);
        }

        public void UpdateTransientState(object state)
        {
            string name = (string)state;
            var item = this.View.FirstOrDefault(A => (A as ActorModel).Name == name);
            if (item != null)
            {
                this.View.MoveCurrentTo(item);
            }
        }
    }
}
